home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 April: Mac OS SDK / Dev.CD Apr 96 SDK / Dev.CD Apr 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc / Sample Code / CALib & You… / Source / CASample / CAS_AppleEvent.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-12-07  |  10.0 KB  |  360 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        CAS_AppleEvent.c
  3.  
  4.     Contains:    Code to install, dispatch, and execute Apple event handlers.
  5.  
  6.     Written by:    David H Nelson
  7.  
  8.     Copyright © 1993-1995 ComponentWorks, All rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.          <2>      9/13/95    TWB        Comment out unnecessary include of CALib. 
  13.          <1>     11/20/93    DHN        Created.
  14.  
  15. */
  16.  
  17. #include <AppleEvents.h>
  18. #include <AERegistry.h>
  19.  
  20. #include "CAS_AppleEvent.h"
  21. #include "CAS_Globals.h"
  22.  
  23. #include "CAS_App.h"
  24. #include "CAS_Doc.h"
  25. #include "CAS_Win.h"
  26. #include "CAS_Error.h"
  27.  
  28. //----------------------------------------------------------------------
  29. // routine descriptors for our event handlers.
  30.  
  31. AEEventHandlerUPP    gOpenApplicationHandlerUPP = nil;
  32. AEEventHandlerUPP    gOpenDocumentsHandlerUPP = nil;
  33. AEEventHandlerUPP    gPrintDocumentsHandlerUPP = nil;
  34. AEEventHandlerUPP    gQuitApplicationHandlerUPP = nil;
  35. AEEventHandlerUPP    gDummyHandlerUPP = nil;
  36.  
  37. //----------------------------------------------------------------------
  38. //     local prototypes
  39.  
  40. #if defined(__cplusplus)
  41. extern "C"
  42. {
  43. #endif
  44.  
  45. static OSErr AE_GotRequiredParams(
  46.     AppleEvent    *theAppleEvent );
  47. pascal OSErr AE_OpenApplicationHandler(
  48.     AppleEvent    *theAppleEvent,
  49.     AppleEvent    *reply,
  50.     long        refCon );
  51. pascal OSErr AE_OpenDocumentsHandler(
  52.     AppleEvent    *theAppleEvent,
  53.     AppleEvent    *reply,
  54.     long        refCon );
  55. pascal OSErr AE_PrintDocumentsHandler(
  56.     AppleEvent    *theAppleEvent,
  57.     AppleEvent    *reply,
  58.     long        refCon );
  59. pascal OSErr AE_QuitApplicationHandler(
  60.     AppleEvent    *theAppleEvent,
  61.     AppleEvent    *reply,
  62.     long        refCon );
  63. pascal OSErr AE_DummyHandler(
  64.     AppleEvent    *theAppleEvent,
  65.     AppleEvent    *reply,
  66.     long        refCon );
  67.  
  68. #if defined(__cplusplus)
  69. }
  70. #endif
  71.  
  72. OSErr    GetDescFSSpec(AEDesc *desc, FSSpec* spec);
  73.  
  74.  
  75. //---------------------------------------------------------------------------
  76. static OSErr AE_GotRequiredParams(
  77.     AppleEvent    *theAppleEvent )
  78. {
  79. DescType    returnedType;
  80. Size        actualSize;
  81. OSErr        theErr;
  82.  
  83.     theErr =
  84.         AEGetAttributePtr(
  85.             theAppleEvent, keyMissedKeywordAttr,
  86.             typeWildCard, &returnedType,
  87.             nil, 0, &actualSize );
  88.  
  89.     // we got all the required parameters
  90.     if (theErr == errAEDescNotFound)
  91.         theErr = noErr;
  92.     // we missed a required parameter
  93.     else if (theErr == noErr)
  94.         theErr = errAEParamMissed;
  95.     // otherwise the call to AEGetAttributePtr failed
  96.  
  97.     return theErr;
  98. }
  99.  
  100. //---------------------------------------------------------------------------
  101. // AE_OpenApplicationHandler - called once at launch if there are NO documents to open.
  102. // Do any setup such as creating a new empty document.
  103. // $$$$$ should we be checking GotRequiredParams here?
  104.  
  105. pascal OSErr AE_OpenApplicationHandler(
  106.     AppleEvent    *theAppleEvent,
  107.     AppleEvent    *reply,
  108.     long        refCon )
  109. {
  110.     App_NewMenu();
  111.  
  112.     return noErr;
  113. }
  114.  
  115. //---------------------------------------------------------------------------
  116. // AE_OpenApplicationHandler - called once at launch if there are documents to open.
  117. // Open the documents that are passed.
  118.  
  119. pascal OSErr AE_OpenDocumentsHandler(
  120.     AppleEvent    *theAppleEvent,
  121.     AppleEvent    *reply,
  122.     long        refCon )
  123. {
  124. AEDescList        docList;
  125. AEKeyword        keyword;
  126. DescType        returnedType;
  127. FSSpec            theFSSpec;
  128. Size            actualSize;
  129. OSErr            theErr;
  130. long            index;
  131. long            itemCount;
  132. AEDesc            documentDesc;
  133.  
  134.     // get the doc list.
  135.     theErr = AEGetParamDesc( theAppleEvent, keyDirectObject, typeAEList, &docList );
  136.     if (theErr != noErr)
  137.         return theErr;
  138.  
  139.     // check for missing required parameters
  140.     theErr = AE_GotRequiredParams( theAppleEvent );
  141.     if (theErr != noErr)
  142.     {
  143.         // an error occurred:  do the necessary error handling
  144.         AEDisposeDesc( &docList );
  145.         return theErr;
  146.     }
  147.  
  148.     // count the number of descriptor records in the list
  149.     theErr = AECountItems( &docList, &itemCount );
  150.     if (theErr != noErr)
  151.         return theErr;
  152.  
  153.     for (index=1; index<=itemCount; index++)
  154.     {
  155.     
  156.         theErr = AEGetNthDesc(&docList, index, typeWildCard,
  157.                 &keyword, &documentDesc);
  158.                 
  159.         if (theErr)
  160.             return theErr;
  161.         
  162.         theErr = GetDescFSSpec(&documentDesc, &theFSSpec);
  163.  
  164.         if (theErr)
  165.             return theErr;
  166.  
  167.         if (theErr != noErr)
  168.             DEBUGSTR( "\pAE_OpenDocumentsHandler: error from AEGetNthPtr" );
  169.  
  170.         // open the file
  171.         theErr = Doc_Open( &theFSSpec );
  172.         if (theErr != noErr)
  173.             Error_ShowMessage( kCASMsgWrongFileVersion );
  174.     }
  175.  
  176.     // get rid of the AEDescList
  177.     theErr = AEDisposeDesc( &docList );
  178.     if (theErr != noErr)
  179.         return theErr;
  180.         
  181.     return theErr;
  182. }
  183.  
  184. //---------------------------------------------------------------------------
  185. // Print the documents that are passed.
  186.  
  187. pascal OSErr AE_PrintDocumentsHandler(
  188.     AppleEvent    *theAppleEvent,
  189.     AppleEvent    *reply,
  190.     long        refCon )
  191. {
  192. AEDescList        docList;
  193. AEKeyword        keyword;
  194. DescType        returnedType;
  195. FSSpec            theFSSpec;
  196. Size            actualSize;
  197. OSErr            theErr;
  198. long            index;
  199. long            itemCount;
  200.  
  201.     // get the doc list.
  202.     theErr = AEGetParamDesc( theAppleEvent, keyDirectObject, typeAEList, &docList );
  203.     if (theErr != noErr)
  204.         return theErr;
  205.  
  206.     // check for missing required parameters
  207.     theErr = AE_GotRequiredParams( theAppleEvent );
  208.     if (theErr != noErr)
  209.     {
  210.         // an error occurred:  do the necessary error handling
  211.         AEDisposeDesc( &docList );
  212.         return theErr;
  213.     }
  214.  
  215.     // count the number of descriptor records in the list
  216.     theErr = AECountItems( &docList, &itemCount );
  217.     if (theErr != noErr)
  218.         DEBUGSTR( "\pPrintDocumentsHandler: error from AECountItems" );
  219.  
  220.     for (index=1; index<=itemCount; index++)
  221.     {
  222.     WindowPtr        theWindow;
  223.  
  224.         // get the ith file in doc list.
  225.         theErr =
  226.             AEGetNthPtr(
  227.                 &docList, index, typeFSS, &keyword, &returnedType,
  228.                 (Ptr)&theFSSpec, (long)sizeof(FSSpec), &actualSize );
  229.         if (theErr != noErr)
  230.             DEBUGSTR( "\pPrintDocumentsHandler: error from AEGetNthPtr");
  231.  
  232.         // open the file
  233.         theErr = Doc_Open( &theFSSpec );
  234.         if (theErr != noErr)
  235.             DEBUGSTR( "\pPrintDocumentsHandler: error from Doc_Open" );
  236.  
  237.         theWindow = App_GetFrontDocWindow();
  238.         if (theWindow != nil)
  239.         {
  240.             // print it and close it
  241.             App_PrintMenu( theWindow );
  242.             Win_Close( theWindow, NULL );
  243.         }
  244.     }
  245.  
  246.     // get rid of the AEDescList
  247.     theErr = AEDisposeDesc( &docList );
  248.     if (theErr != noErr)
  249.         DEBUGSTR("\pPrintDocumentsHandler: error from AEDisposeDesc" );
  250.  
  251.     return noErr;
  252. }
  253.  
  254. //---------------------------------------------------------------------------
  255. // Do any cleanup before quiting and set the gExitFlag
  256. // $$$$$ This routine should get the Save Options and decide whether to save
  257. // all docs, don't save, or ask the user.
  258. pascal OSErr AE_QuitApplicationHandler(
  259.     AppleEvent    *theAppleEvent,
  260.     AppleEvent    *reply,
  261.     long        refCon )
  262. {
  263.     gExitFlag = App_CloseAll();
  264.     return noErr;
  265. }
  266.  
  267. //---------------------------------------------------------------------------
  268. // Place holder handler that does nothing but return errAEEventNotHandled
  269.  
  270. pascal OSErr AE_DummyHandler(
  271.     AppleEvent    *theAppleEvent,
  272.     AppleEvent    *reply,
  273.     long        refCon )
  274. {
  275.     return errAEEventNotHandled;
  276. }
  277.  
  278. //---------------------------------------------------------------------------
  279.  
  280. void AE_InitAppleEvents( void )
  281. {
  282.     OSErr    err;
  283.  
  284.     // Create the routine descriptors and install the handlers for the Required Suite
  285.     gOpenApplicationHandlerUPP = NewAEEventHandlerProc( AE_OpenApplicationHandler );
  286.     err = AEInstallEventHandler( kCoreEventClass, kAEOpenApplication, gOpenApplicationHandlerUPP, 0, false );
  287.  
  288.     if (err) DebugStr ("\p AEInstallEventHandler error");
  289.     
  290.     gOpenDocumentsHandlerUPP = NewAEEventHandlerProc( AE_OpenDocumentsHandler );
  291.     err = AEInstallEventHandler( kCoreEventClass, kAEOpenDocuments, gOpenDocumentsHandlerUPP, 0, false );
  292.     
  293.     if (err) DebugStr ("\p AEInstallEventHandler error");
  294.     
  295.     gPrintDocumentsHandlerUPP = NewAEEventHandlerProc( AE_PrintDocumentsHandler );
  296.     err = AEInstallEventHandler( kCoreEventClass, kAEPrintDocuments, gPrintDocumentsHandlerUPP, 0, false );
  297.     
  298.     if (err) DebugStr ("\p AEInstallEventHandler error");
  299.     
  300.     gQuitApplicationHandlerUPP = NewAEEventHandlerProc( AE_QuitApplicationHandler );
  301.     err = AEInstallEventHandler( kCoreEventClass, kAEQuitApplication, gQuitApplicationHandlerUPP, 0, false );
  302.     
  303.     if (err) DebugStr ("\p AEInstallEventHandler error");
  304.     
  305. #if 0
  306.     // $$$$$ each of these handlers needs a different type of routine descriptor.
  307.     // Create the routine descriptors and install the handlers for the Core Suite
  308.     gDummyHandlerUPP = NewAEEventHandlerProc( OpenApplicationHandler );
  309.     AEInstallEventHandler( kAECoreSuite, kAEClone,            gDummyHandlerUPP, 0, false );
  310.     AEInstallEventHandler( kAECoreSuite, kAEClose,            gDummyHandlerUPP, 0, false );
  311.     AEInstallEventHandler( kAECoreSuite, kAECountElements,    gDummyHandlerUPP, 0, false );
  312.     AEInstallEventHandler( kAECoreSuite, kAECreateElement,    gDummyHandlerUPP, 0, false );
  313.     AEInstallEventHandler( kAECoreSuite, kAEDelete,            gDummyHandlerUPP, 0, false );
  314.     AEInstallEventHandler( kAECoreSuite, kAEDoObjectsExist,    gDummyHandlerUPP, 0, false );
  315.     AEInstallEventHandler( kAECoreSuite, kAEGetData,        gDummyHandlerUPP, 0, false );
  316.     AEInstallEventHandler( kAECoreSuite, kAEGetDataSize,    gDummyHandlerUPP, 0, false );
  317.     AEInstallEventHandler( kAECoreSuite, kAEGetClassInfo,    gDummyHandlerUPP, 0, false );
  318.     AEInstallEventHandler( kAECoreSuite, kAEGetEventInfo,    gDummyHandlerUPP, 0, false );
  319.     AEInstallEventHandler( kAECoreSuite, kAEMove,             gDummyHandlerUPP, 0, false );
  320.     AEInstallEventHandler( kAECoreSuite, kAESave,            gDummyHandlerUPP, 0, false );
  321.     AEInstallEventHandler( kAECoreSuite, kAESetData,        gDummyHandlerUPP, 0, false );
  322. #endif
  323. }
  324.  
  325. //---------------------------------------------------------------------------
  326.  
  327. void AE_DisposeAppleEvents( void )
  328. {
  329.     DisposeRoutineDescriptor( gOpenApplicationHandlerUPP );
  330.     DisposeRoutineDescriptor( gOpenDocumentsHandlerUPP );
  331.     DisposeRoutineDescriptor( gPrintDocumentsHandlerUPP );
  332.     DisposeRoutineDescriptor( gQuitApplicationHandlerUPP );
  333. //    DisposeRoutineDescriptor( gDummyHandlerUPP );
  334. }
  335.  
  336. //---------------------------------------------------------------------------
  337.  
  338. void AE_DoAppleEvent(
  339.     EventRecord        *theEvent )
  340. {
  341.     AEProcessAppleEvent( theEvent );
  342. }
  343.  
  344.  
  345.  
  346. //==============================================================================
  347. // GetDescFSSpec
  348. //==============================================================================
  349.  
  350. OSErr
  351. GetDescFSSpec(AEDesc *desc, FSSpec* spec)
  352. {
  353.     AEDesc resultDesc;
  354.     OSErr err = AECoerceDesc(desc, typeFSS, &resultDesc);
  355.     if (err) return err;
  356.     *spec = **(FSSpec**)(resultDesc.dataHandle);
  357.     AEDisposeDesc(&resultDesc);
  358.     return noErr;
  359. }
  360.